package com.ffs.persist.dao;

import com.ffs.persist.IPersistFactory;
import com.ffs.persist.PersistTable;
import com.ffs.persist.SqlBuilder;
import com.ffs.persist.jdbc.JdbcTemplate;
import com.ffs.persist.jdbc.RowMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ffs80
 * @date 2022/4/16 11:10
 */
public class PersistDbDao<T extends PersistTable>{

    private String tableName;

    private SqlBuilder<T> sqlBuilder;

    private JdbcTemplate jdbcTemplate;

    private RowMapper<T> rowMapper;

    public PersistDbDao(JdbcTemplate jdbcTemplate, Class<T> clazz, IPersistFactory factory) {
        this.tableName = findTableName(clazz);
        this.jdbcTemplate = jdbcTemplate;
        this.sqlBuilder = factory.findSqlBuilder(clazz);
        this.rowMapper = factory.rowMapper(clazz);
    }

    /**
     * 单体查询
     */
    public T query(String sql,Object... params){
        return jdbcTemplate.query(sql,rowMapper,params);
    }

    /**
     * 根据id查询
     */
    public T selectById(Object id){
        return query(sqlBuilder.buildSelectByIdSql(tableName),id);
    }

    /**
     * 批量查询
     */
    public List<T> queryList(String sql,Object... params){
        return jdbcTemplate.queryList(sql,rowMapper,params);
    }

    /**
     * 单体插入
     */
    public void insert(T data){
        String sql = sqlBuilder.buildInsertSql(tableName);
        Object[] params = sqlBuilder.buildInsertParams(data);
        jdbcTemplate.update(sql,params);
    }

    /**
     * 批量插入
     */
    public void batchInsert(List<T> dataList){
        String sql = sqlBuilder.buildInsertSql(tableName);
        List<Object[]> params = new ArrayList<>();
        for (T data : dataList){
            Object[] param = sqlBuilder.buildInsertParams(data);
            params.add(param);
        }
        jdbcTemplate.updateBatch(sql,params);
    }

    /**
     * 单体更新
     */
    public void update(T data){
        String sql = sqlBuilder.buildUpdateSql(tableName);
        Object[] params = sqlBuilder.buildUpdateParams(data);
        jdbcTemplate.update(sql,params);
    }

    /**
     * 批量更新
     */
    public void batchUpdate(List<T> dataList){
        String sql = sqlBuilder.buildUpdateSql(tableName);
        List<Object[]> params = new ArrayList<>();
        for (T data : dataList){
            Object[] param = sqlBuilder.buildUpdateParams(data);
            params.add(param);
        }
        jdbcTemplate.updateBatch(sql,params);
    }

    /**
     * 单体删除
     */
    public void delete(T data){
        String sql = sqlBuilder.buildDeleteSql(tableName);
        Object[] params = sqlBuilder.buildDeleteParams(data);
        jdbcTemplate.update(sql,params);
    }

    /**
     * 批量删除
     */
    public void batchDelete(List<T> dataList){
        String sql = sqlBuilder.buildDeleteSql(tableName);
        List<Object[]> params = new ArrayList<>();
        for (T data : dataList){
            Object[] param = sqlBuilder.buildDeleteParams(data);
            params.add(param);
        }
        jdbcTemplate.updateBatch(sql,params);
    }


    /**
     * 根据类名查找表名
     */
    private String findTableName(Class<T> clazz){

        return null;
    }
}
